Sunburst Charts with Plotly - Part 2

Column

Text

Controlling text orientation inside sunburst sectors
  • The insidetextorientation attribute controls the orientation of text inside sectors.

  • With “auto” the texts may automatically be rotated to fit with the maximum size inside the slice. Using “horizontal” (resp. “radial”, “tangential”) forces text to be horizontal (resp. radial or tangential). Note that plotly may reduce the font size in order to fit the text with the requested orientation.

df = read.csv("https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/coffee-flavors.csv")


head(df)
##                         ids     labels parents
## 1         Enzymatic-Flowery    Flowery        
## 2          Enzymatic-Fruity     Fruity        
## 3           Enzymatic-Herby      Herby        
## 4      Sugar Browning-Nutty      Nutty        
## 5   Sugar Browning-Carmelly   Carmelly        
## 6 Sugar Browning-Chocolatey Chocolatey
fig <- plot_ly()
fig <- fig %>% add_trace(

  type='sunburst',

  ids=df$ids,

  labels=df$labels,

  parents=df$parents,

  domain=list(column=1),

  maxdepth=2,

  insidetextorientation='radial'

)

Output

fig

Subplots

In order to create sunburst chart subplots, we use the domain attribute and the layout grid attribute.

library(plotly)


d1 <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/coffee-flavors.csv")

d2 <- read.csv("https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv")
fig <- plot_ly() 

fig <- fig %>%

  add_trace(

    ids = d1$ids,

    labels = d1$labels,

    parents = d1$parents,

    type = 'sunburst',

    maxdepth = 2,

    domain = list(column = 0)

    ) 
fig <- fig %>%

  add_trace(

    ids = d2$ids,

    labels = d2$labels,

    parents = d2$parents,

    type = 'sunburst',

    maxdepth = 3,

    domain = list(column = 1)

  ) 

fig <- fig %>%

    layout(

      grid = list(columns =2, rows = 1),

      margin = list(l = 0, r = 0, b = 0, t = 0),

      sunburstcolorway = c(

        "#636efa","#EF553B","#00cc96","#ab63fa","#19d3f3",

        "#e763fa", "#FECB52","#FFA15A","#FF6692","#B6E880"

      ),

      extendsunburstcolors = TRUE)

Output

fig
## Warning: 'layout' objects don't have these attributes: 'sunburstcolorway', 'extendsunburstcolors'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'

Reference

See https://plotly.com/r/reference/#sunburst for more information and chart attribute options! What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

Dash


library(plotly)


fig <- plot_ly() 

# fig <- fig %>% add_trace( ... )

# fig <- fig %>% layout( ... ) 


library(dash)

library(dashCoreComponents)

library(dashHtmlComponents)


app <- Dash$new()

app$layout(

    htmlDiv(

        list(

            dccGraph(figure=fig) 

        )

     )

)


app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)